﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    public sealed class UserSystemNoLocalDataClient : UserSystemNoLocalData
    {
        private const string _REGISTERED_BY_ADMIN_MENU = "权限设置\n1. 用户\n2. 管理员\n0. 退出添加用户";

        public override void AddUserMenu(int role = RoleConstants.USER)
        {
            Console.Write("请输入用户名: ");
            var username = Console.ReadLine();
            Console.Write("请输入密码: ");
            var password = Console.ReadLine();
            // 数据校验
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                Console.WriteLine("请填入有效信息!");
                return;
            }

            if (role == RoleConstants.USER) AddUser(username, password, RoleConstants.USER);
            else if (role == RoleConstants.ADMIN) RegisterUserByAdmin(username, password);
        }

        public override void DeleteUserMenu()
        {
            Console.Write("请输入用户名: ");
            var username = Console.ReadLine();
            DeleteUser(username);
        }

        public override void ChangePasswordMenu()
        {
            var (flag, user) = JudgeUserConsole();
            if (flag)
            {
                while (true)
                {
                    Console.Clear();
                    Console.Write("请输入新密码: ");
                    var newPassword = Console.ReadLine();
                    if (!string.IsNullOrEmpty(newPassword))
                    {
                        ChangePassword(user, newPassword);
                        return;
                    }
                    Console.WriteLine("请输入有效密码!");
                    Console.WriteLine("按任意键继续...");
                    Console.ReadKey();
                }
            }
        }

        public override void ResetPasswordMenu()
        {
            Console.Write("请输入用户名: ");
            var username = Console.ReadLine();
            ResetPassword(username);
        }

        private void RegisterUserByAdmin(string username, string password)
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine(_REGISTERED_BY_ADMIN_MENU);
                Console.Write("请选择操作 (0-2): ");

                var choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        AddUser(username, password, RoleConstants.USER);
                        return;
                    case "2":
                        AddUser(username, password, RoleConstants.ADMIN);
                        return;
                    case "0":
                        Console.WriteLine("终止添加用户...");
                        return;
                    default:
                        Console.WriteLine("无效的选项, 请重新选择!");
                        break;
                }
                Console.WriteLine("按任意键返回主菜单...");
                Console.ReadKey();
            }
        }
    }
}
